﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tea.Controllers;
using tea.DB;
using tea.Models;

namespace tea.BL
{
    public class BL게시판
    {
        public const int 공지사항ID = 1;
        public const int 게시글분류_전체 = 0;

        public const int 검색_없음 = 0;
        public const int 검색_제목 = 1;
        public const int 검색_제목내용 = 2;
        public const int 검색_작성자 = 3;

        public static SelectList 검색유형목록 = new SelectList(
            new List<SelectListItemModel> { 
                new SelectListItemModel(BL게시판.검색_없음, "검색 없음"),
                new SelectListItemModel(BL게시판.검색_제목, "제목"),
                new SelectListItemModel(BL게시판.검색_제목내용, "제목+내용"),
                new SelectListItemModel(BL게시판.검색_작성자, "작성자")
            },
            "ID", "Text"
        );

        public static List<DB.게시판> 메뉴목록()
        {
            using (var db = new DBDataContext())
            {
                return db.게시판.ToList();
            }
        }

        public static List<DB.게시글분류> 게시글분류목록(int id, bool 전체항목_포함 = false)
        {
            using (var db = new DBDataContext())
            {
                var list = db.게시글분류.Where(p => p.게시판ID == id).OrderByDescending(p => p.게시글.Count()).ThenBy(p => p.제목).ToList();
                var n = new 게시글분류 { 게시판ID = id, 제목 = Consts.미분류 };
                if (list.Count <= 0) {
                    db.게시글분류.InsertOnSubmit(n);
                    db.SubmitChanges();
                    list.Add(n);
                }
                if (전체항목_포함) list.Insert(0, new DB.게시글분류 { ID = 게시글분류_전체, 제목 = "전체" });
                return list;
            }
        }

        public static bool 글수정권한(DB.게시글 글)
        {
            return 글.작성자ID == BL사용자.현재사용자.ID || BL사용자.IsAdmin();
        }
    }

    public static class BbsLinkHekper
    {
        public static string BbsList(this UrlHelper url, PaginationModel pagination)
        {
            if (url == null) new BbsController().List(pagination: pagination);
            return url.Action("List", "Bbs", pagination);
        }

        public static string BbsListInit(this UrlHelper url, int id)
        {
            if (url == null) new BbsController().List(pagination: new PaginationModel());
            var pagination = new PaginationModel();
            Util.BeanUtil.속성복사(소스: PaginationModel.DEFAULT, 타겟: pagination);
            pagination.ID = id;
            return url.Action("List", "Bbs", pagination);
        }

        public static string BbsList(this UrlHelper url, int id)
        {
            if (url == null) new BbsController().List(pagination: new PaginationModel());
            return url.Action("List", "Bbs", new PaginationModel { ID = id });
        }

        public static string BbsListCategory(this UrlHelper url, int id, int category)
        {
            if (url == null) new BbsController().List(pagination: new PaginationModel());
            return url.Action("List", "Bbs", new PaginationModel { ID = id, page = 1, category = category });
        }

        public static string BbsListResize(this UrlHelper url, int id, int size)
        {
            if (url == null) new BbsController().List(pagination: new PaginationModel());
            return url.Action("List", "Bbs", new PaginationModel { ID = id, page = 1, size = size });
        }

        public static string BbsListSearch(this UrlHelper url, int id, int srchType, string srchText)
        {
            if (url == null) new BbsController().List(pagination: new PaginationModel());
            return url.Action("List", "Bbs", new PaginationModel { ID = id, page = 1, srchType = srchType, srchText = srchText });
        }

        public static string BbsNew(this UrlHelper url, int id)
        {
            if (url == null) new BbsController().New(id: 0);
            return url.Action("New", "Bbs", new { id = id });
        }

        public static string BbsEdit(this UrlHelper url, long id)
        {
            if (url == null) new BbsController().New(id: 0);
            return url.Action("Edit", "Bbs", new { id = id });
        }

        public static string BbsArticle(this UrlHelper url, long id)
        {
            if (url == null) new BbsController().Article(id: 0);
            return url.Action("Article", "Bbs", new { id = id });
        }

        public static string BbsAdminEdit(this UrlHelper url, int id)
        {
            if (url == null) new BbsAdminController().Edit(id: 0);
            return url.Action("Edit", "BbsAdmin", new { id = id });
        }
    }

}